package com.fuyin.controller.admin;

import com.fuyin.commons.aop.OperationLogAnnotation;
import com.fuyin.commons.aop.RequestLogAnnotation;
import com.fuyin.commons.response.BaseResponse;
import com.fuyin.commons.response.ErrorCode;
import com.fuyin.commons.utils.MyUtils;
import com.fuyin.dto.params.LoginParam;
import com.fuyin.dto.vo.*;
import com.fuyin.entity.Fxb;
import com.fuyin.entity.OperationLog;
import com.fuyin.entity.ReportInfo;
import com.fuyin.entity.User;
import com.fuyin.service.RedisService;
import com.fuyin.service.admin.AdminService;
import com.fuyin.service.user.GpService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yuanxilan (yuanxilan@keep.com)
 * @Description
 * @date 2021年10月23日 2:09 下午
 * @since
 */
@Api("管理员接口")
@Controller
@RequestMapping(value = "/admin")
@Slf4j
public class AdminController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private GpService gpService;

    @Autowired
    private RedisService redisService;

    @ApiOperation("管理员登陆")
    @PostMapping(value = "/login")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "登陆成功"),
            @ApiResponse(code = 400, message = "参数不全"),
            @ApiResponse(code = 412, message = "账号不存在"),
            @ApiResponse(code = 413, message = "账号未启用"),
            @ApiResponse(code = 414, message = "账号密码错误"),
            @ApiResponse(code = 423, message = "验证码错误"),
            @ApiResponse(code = 424, message = "验证码超时")
    })
    @ResponseBody
    @OperationLogAnnotation(operType = "登录日志", operDesc = "登录")
    @RequestLogAnnotation
    public BaseResponse login(@RequestBody LoginParam loginParam, HttpServletRequest request) {
        MyUtils.checkParams("验证码和 key 不能为空", loginParam.getCode(), loginParam.getKey());
        if (!redisService.exists(loginParam.getKey())) {
            return BaseResponse.error(ErrorCode.VerificationCode_TIMEOUT, "验证码超时");
        }
        if (!redisService.get(loginParam.getKey()).equals(loginParam.getCode())) {
            return BaseResponse.error(ErrorCode.VerificationCode_ERROR, "验证码错误");
        }
        log.info("request:{}", MyUtils.getIpAddr(request));
        return adminService.login(loginParam);
    }

    @GetMapping(value = "/test")
    @ResponseBody
    public BaseResponse test(@RequestHeader("Authorization") String token) {
//        log.info("token: {}", token);
        BaseResponse<ArrayList<ReportInfo>> reportInfo = adminService.getReportInfo(token, 100000, 100000, 0);
        log.info("reportInfo: {}", reportInfo.getData().size());
        List<ReportInfo> collect = reportInfo.getData().stream().filter(x -> x.getTitle().contains("1")).collect(Collectors.toList());
        log.info("collect: {}", collect.size());
        try {
            RiskReport riskReport = (RiskReport) MyUtils.byteToObj(collect.get(0).getInfo());
            log.info("riskReport: {}", riskReport);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return BaseResponse.success();
    }

    @ApiOperation("管理员登出")
    @PostMapping(value = "/logout")
    @ResponseBody
    @RequestLogAnnotation
    @OperationLogAnnotation(operType = "登录日志", operDesc = "登出")
    public BaseResponse logout(@RequestHeader("Authorization") String token) {
        return adminService.logout(token);
    }

    @ApiOperation("管理员修改用户密码")
    @PostMapping(value = "/updateuserpassword")
    @ResponseBody
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "修改成功"),
            @ApiResponse(code = 400, message = "参数不全"),
            @ApiResponse(code = 403, message = "只能修改用户密码"),
            @ApiResponse(code = 412, message = "账号不存在"),
            @ApiResponse(code = 417, message = "新旧密码不能相同")
    })
    @RequestLogAnnotation
    public BaseResponse updateUserPassword(@RequestHeader("Authorization") String token, @RequestBody LoginParam param, HttpServletRequest request) {
        return adminService.updateUserPassword(token, param.getUserName(), param.getPassword(), request);
    }

    @ApiOperation("管理员获取风险指标")
    @GetMapping("/getzb")
    @ResponseBody
    @RequestLogAnnotation
    public BaseResponse<ZbDto> getZB(@RequestHeader("Authorization") String token) {
        return adminService.getZB();
    }

    @ApiOperation("管理员自定义风险指标")
    @PostMapping("/updatezb")
    @ResponseBody
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "修改成功"),
            @ApiResponse(code = 400, message = "不能所有指标都为空")
    })
    @RequestLogAnnotation
    @OperationLogAnnotation(operType = "操作日志", operDesc = "修改自定义风险指标")
    public BaseResponse updateZB(@RequestHeader("Authorization") String token, @RequestBody ArrayList<ZdyzbDto> zdyzbs) {
        return adminService.updateZB(zdyzbs);
    }

    @ApiOperation("管理员获取自定义模型指标")
    @GetMapping("/getzdyzbmodel")
    @ResponseBody
    @RequestLogAnnotation
    public BaseResponse<ArrayList<ZdyzbModel>> getZdyzbModel(@RequestHeader("Authorization") String token) {
        return adminService.getZdyzbModel();
    }

    @ApiOperation("管理员自定义模型指标")
    @PostMapping("/updatezdyzbmodel")
    @ResponseBody
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "修改成功"),
            @ApiResponse(code = 400, message = "不能所有指标都为空")
    })
    @RequestLogAnnotation
    @OperationLogAnnotation(operType = "操作日志", operDesc = "修改自定义模型指标")
    public BaseResponse updateZdyzbModel(@RequestHeader("Authorization") String token, @RequestBody ZdyzbModel zdyzbModel) {
        return adminService.updateZdyzbModel(token, zdyzbModel);
    }

    @ApiOperation("管理员修改融资匹配度 (先获取所有融资匹配度，再根据相同的格式发送给后端)")
    @PostMapping("/updaterzppd")
    @ResponseBody
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "修改成功"),
            @ApiResponse(code = 400, message = "不能所有指标都为空")
    })
    @RequestLogAnnotation
    @OperationLogAnnotation(operType = "操作日志", operDesc = "修改融资匹配度指标")
    public BaseResponse updateRzppd(@RequestHeader("Authorization") String token, @RequestBody List<Rzppd> rzppds) {
        return adminService.updateRzppd(token, rzppds);
    }

    @ApiOperation("获取个人信息")
    @ResponseBody
    @GetMapping("/getuserinfo")
    @RequestLogAnnotation
    public BaseResponse<User> getUserInfo(@RequestHeader("Authorization") String token) {
        return adminService.getUserInfo(token);
    }

    @ApiOperation("管理员获取风险表")
    @GetMapping("/getfxbs")
    @ResponseBody
    @RequestLogAnnotation
    public BaseResponse<ArrayList<Fxb>> getFxbs(@RequestHeader("Authorization") String token) {
        return adminService.getFxbs();
    }

    @ApiOperation("管理员修改风险表")
    @PostMapping("/updatefxbs")
    @ResponseBody
    @RequestLogAnnotation
    @OperationLogAnnotation(operType = "操作日志", operDesc = "修改风险表")
    public BaseResponse updateFxbs(@RequestHeader("Authorization") String token, @RequestBody ArrayList<Fxb> fxbs) {
        return adminService.updateFxbs(fxbs);
    }

    @ApiOperation("管理员获取融资匹配度")
    @GetMapping("/getrzppd")
    @ResponseBody
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "获取成功")
    })
    @RequestLogAnnotation
    public BaseResponse<ArrayList<Rzppd>> getRzppd(@RequestHeader("Authorization") String token) {
        return adminService.getRzppd(token);
    }

    @ResponseBody
    @ApiOperation("获取模型评估能力分析")
    @PostMapping("getmodelanalysis")
    @RequestLogAnnotation
    public BaseResponse<String> getModelAnalysis(@RequestHeader("Authorization") String token, @RequestBody GpDetails gpDetails) {
        return gpService.getModelAnalysis(gpDetails);
    }

    @ApiOperation("获取风险控制报告")
    @GetMapping(value = "/getriskreport")
    @ResponseBody
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "获取成功"),
            @ApiResponse(code = 400, message = "参数不全"),
            @ApiResponse(code = 418, message = "抓取不到股票信息"),
            @ApiResponse(code = 421, message = "暂时不支持该股票的爬取")
    })
    @RequestLogAnnotation
    @OperationLogAnnotation(operType = "操作日志", operDesc = "获取风险控制报告")
    public BaseResponse<RiskReport> getRiskReport(@RequestHeader("Authorization") String token, @RequestParam @ApiParam("股票名") String gpName) {
        return gpService.getRiskReport(token, gpName);
    }

    @ApiOperation("分页获取报告记录")
    @GetMapping(value = "/getreportinfo/{pageNum:[0-9]+}/{pageSize:[0-9]+}/{type:[0-1]+}")
    @ResponseBody
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "获取成功"),
            @ApiResponse(code = 400, message = "请求参数有误"),
            @ApiResponse(code = 401, message = "token 失效，请重新登录"),
            @ApiResponse(code = 412, message = "账号不存在")
    })
    @RequestLogAnnotation
    @OperationLogAnnotation(operType = "操作日志", operDesc = "获取报告记录")
    public BaseResponse<ArrayList<ReportInfo>> getReportInfo(@RequestHeader("Authorization") String token, @PathVariable Integer pageNum, @PathVariable Integer pageSize, @PathVariable Integer type) {
        return adminService.getReportInfo(token, pageNum, pageSize, type);
    }

    @ApiOperation("获取融资方案分析报告")
    @PostMapping(value = "/getmodelreport")
    @ResponseBody
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "获取成功"),
            @ApiResponse(code = 400, message = "参数不全"),
            @ApiResponse(code = 418, message = "抓取不到股票信息"),
            @ApiResponse(code = 421, message = "暂时不支持该股票的爬取")
    })
    @RequestLogAnnotation
    @OperationLogAnnotation(operType = "操作日志", operDesc = "获取融资方案分析报告")
    public BaseResponse<ModelReport> getModelReport(@RequestHeader("Authorization") String token, @RequestParam @ApiParam("股票名") String gpName, @RequestBody RzNeed rzNeed) {
        return gpService.getModelReport(token, rzNeed, gpName);
    }

    @ResponseBody
    @ApiOperation("获取能力分析")
    @PostMapping("/getanalysis")
    @RequestLogAnnotation
    public BaseResponse<ArrayList<RiskReport.Analysis>> getAnalysis(@RequestHeader("Authorization") String token, @RequestBody GpDetails gpDetails) {
        return gpService.getAnalysis(gpDetails);
    }

    @ApiOperation("分页获取所有用户")
    @GetMapping(value = "/getallusers/{pageNum:[0-9]+}/{pageSize:[0-9]+}")
    @ResponseBody
    @RequestLogAnnotation
    public BaseResponse<ArrayList<User>> getAllUsers(@RequestHeader("Authorization") String token, @PathVariable Integer pageNum, @PathVariable Integer pageSize) {
        return adminService.getAllUsers(token, pageNum, pageSize);
    }

    @ApiOperation("修改用户信息")
    @PostMapping(value = "/updateuserinfo")
    @ResponseBody
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "更新成功"),
            @ApiResponse(code = 406, message = "请求过于频繁")
    })
    @RequestLogAnnotation
    public BaseResponse updateUserInfo(@RequestHeader("Authorization") String token, @RequestBody User user, HttpServletRequest request) {
        return adminService.updateUserInfo(token, user, request);
    }

    @ApiOperation("分页获取操作日志")
    @GetMapping(value = "/getoperationlogs/{pageNum:[0-9]+}/{pageSize:[0-9]+}")
    @ResponseBody
    @RequestLogAnnotation
    @OperationLogAnnotation(operType = "操作日志", operDesc = "获取操作日志")
    public BaseResponse<ArrayList<OperationLog>> getOperationLogs(@RequestHeader("Authorization") String token, @PathVariable Integer pageNum, @PathVariable Integer pageSize) {
        return adminService.getOperationLogs(token, pageNum, pageSize);
    }

    @OperationLogAnnotation(operType = "操作日志", operDesc = "导出报告")
    @ApiOperation("保存生成报告记录")
    @PostMapping("/saveriskreportinfo")
    @ResponseBody
    @RequestLogAnnotation
    public BaseResponse saveRiskReportInfo(@RequestHeader("Authorization") String token, @RequestBody RiskReport riskReport) {
        return gpService.saveRiskReportInfo(token, riskReport);
    }

    @OperationLogAnnotation(operType = "操作日志", operDesc = "导出报告")
    @ApiOperation("保存生成报告记录")
    @PostMapping("/savemodelreportinfo")
    @ResponseBody
    @RequestLogAnnotation
    public BaseResponse saveModelReportInfo(@RequestHeader("Authorization") String token, @RequestBody ModelReport modelReport) {
        return gpService.saveModelReportInfo(token, modelReport);
    }
}
